{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <h2>综合评定</h2>
        
        {% if current_user.role == 'student' %}
            <!-- 学生视图 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>我的综合评定成绩</h4>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学期</th>
                                <th>学业成绩</th>
                                <th>德育素质</th>
                                <th>体育素质</th>
                                <th>能力素质</th>
                                <th>劳育素质</th>
                                <th>总分</th>
                                <th>班级排名</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for eval in evaluations %}
                            <tr>
                                <td>{{ eval.semester }}</td>
                                <td>{{ eval.academic_score }}</td>
                                <td>{{ eval.moral_score }}</td>
                                <td>{{ eval.physical_score }}</td>
                                <td>{{ eval.ability_score }}</td>
                                <td>{{ eval.labor_score }}</td>
                                <td>{{ eval.total_score }}</td>
                                <td>{{ eval.rank if eval.rank else '暂无' }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% elif current_user.role == 'teacher' %}
            <!-- 教师视图 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>{{ current_user.class_name }}学生综合评定</h4>
                </div>
                <div class="card-body">
                    <!-- 学期选择器 -->
                    <div class="mb-3">
                        <label class="form-label">选择学期</label>
                        <select class="form-select" id="semesterSelect" onchange="filterBySemester(this.value)">
                            <option value="all">所有学期</option>
                            {% for semester in semesters %}
                            <option value="{{ semester }}">{{ semester }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <!-- 成绩表格 -->
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学号</th>
                                <th>姓名</th>
                                <th>学期</th>
                                <th>学业成绩</th>
                                <th>德育素质</th>
                                <th>体育素质</th>
                                <th>能力素质</th>
                                <th>劳育素质</th>
                                <th>总分</th>
                                <th>排名</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for eval in class_evaluations %}
                            <tr class="semester-row" data-semester="{{ eval.semester }}">
                                <td>{{ eval.student.username }}</td>
                                <td>{{ eval.student.real_name }}</td>
                                <td>{{ eval.semester }}</td>
                                <td>{{ eval.academic_score }}</td>
                                <td>{{ eval.moral_score }}</td>
                                <td>{{ eval.physical_score }}</td>
                                <td>{{ eval.ability_score }}</td>
                                <td>{{ eval.labor_score }}</td>
                                <td>{{ eval.total_score }}</td>
                                <td>{{ loop.index }}</td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 成绩分析 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>成绩分析</h4>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h5>班级平均分</h5>
                            <table class="table">
                                <tr>
                                    <th>学业成绩</th>
                                    <td>{{ class_stats.academic_avg|round(2) }}</td>
                                </tr>
                                <tr>
                                    <th>德育素质</th>
                                    <td>{{ class_stats.moral_avg|round(2) }}</td>
                                </tr>
                                <tr>
                                    <th>体育素质</th>
                                    <td>{{ class_stats.physical_avg|round(2) }}</td>
                                </tr>
                                <tr>
                                    <th>能力素质</th>
                                    <td>{{ class_stats.ability_avg|round(2) }}</td>
                                </tr>
                                <tr>
                                    <th>劳育素质</th>
                                    <td>{{ class_stats.labor_avg|round(2) }}</td>
                                </tr>
                                <tr>
                                    <th>总分</th>
                                    <td>{{ class_stats.total_avg|round(2) }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <h5>分数分布</h5>
                            <table class="table">
                                <tr>
                                    <th>90分以上</th>
                                    <td>{{ class_stats.above_90 }}人</td>
                                </tr>
                                <tr>
                                    <th>80-89分</th>
                                    <td>{{ class_stats.above_80 }}人</td>
                                </tr>
                                <tr>
                                    <th>70-79分</th>
                                    <td>{{ class_stats.above_70 }}人</td>
                                </tr>
                                <tr>
                                    <th>60-69分</th>
                                    <td>{{ class_stats.above_60 }}人</td>
                                </tr>
                                <tr>
                                    <th>60分以下</th>
                                    <td>{{ class_stats.below_60 }}人</td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
function filterBySemester(semester) {
    const rows = document.querySelectorAll('.semester-row');
    rows.forEach(row => {
        if (semester === 'all' || row.dataset.semester === semester) {
            row.style.display = '';
        } else {
            row.style.display = 'none';
        }
    });
}
</script>
{% endblock %} 